﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KzwgRoomTester
{

    public delegate void LoggerDumpHandler(string text);
    class Logger
    {
        private static LoggerDumpHandler _GlobalLogHandler;
        private static object locker = new object();

        public static void Setup(LoggerDumpHandler dumpHandler)
        {
            lock (locker)
            {
                if (_GlobalLogHandler == null)
                {
                    _GlobalLogHandler = dumpHandler;
                }
                else
                {
                    _GlobalLogHandler += dumpHandler;
                }
            }

        }


        public static void LogException(string format, params object[] args)
        {
            Log("EXCEPTION", string.Format(format, args));
        }

        public static void LogError(string format, params object[] args)
        {
            Log("ERROR", string.Format(format, args));
        }


        public static void LogException(Exception e)
        {
            Log("EXCEPTION", e.Message);
            Log("Stack", e.StackTrace);
        }

        public static void LogInfo(string format, params object[] args)
        {
            Log("INFO", string.Format(format, args));
        }

        public static void LogWarning(string format, params object[] args)
        {
            Log("WARING", string.Format(format, args));
        }

        public static void Log(string label, string text)
        {
            if (_GlobalLogHandler != null)
            {
                //lock (locker)
                //{
                _GlobalLogHandler.Invoke("---> [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] ");
                _GlobalLogHandler.Invoke("[" + label + "] ");
                _GlobalLogHandler.Invoke(text);
                _GlobalLogHandler.Invoke(Environment.NewLine);
                //}
            }
        }



        public static void ShowTip(string msg)
        {
            MessageBox.Show(msg);
        }
    }
}
